<?php
namespace app\shell;


use share\console\CConsole;
use share\console\CLIRunner;
use app\controller\api\UserController;
use app\service\DeviceBaseService;
use app\service\UserDeviceBindingService;
use app\service\UserDeviceShareService;
use share\exception\ApiException;
use share\exception\Errno;
use share\service\AmqpService;
use share\service\IpLocation;
use share\service\NotifyDingTalkService;
use share\service\RedisLockService;
use share\service\SendCodeHistoryService;
use share\service\SendCodeService;
use share\service\SendEmailProxy;
use share\utils\Helper;

class CTest extends CLIRunner{

    public function test_action(){ 



        // $data = SendNoticeTableCacheService::getAllByFilter([]);

        // $data = DeviceGroupTableCacheService::getAllByFilter(['user_id' => '123456789']);

        // $data = UserInfoCacheService::mget(["001b11f43b90c2990a0804b700f06582","0002434224eb91cf3d0c450be2630743","0002434224eb91cf3d0c450be2630743","000d0b7a35d29e3470ceeee1dd928169"]);
        // UserInfoCacheService::mget(["123","456","789","000d0b7a35d29e3470ceeee1dd928169"]);
        // UserInfoCacheService::reflush(['user_id'=>"000d0b7a35d29e3470ceeee1dd928169"]);
        // return $data;

        // $info = AdminPathinfoService::getRoleInfoByUser(1);
        // $items = [
        //     ['aid'=>0,'rid'=> 1,'first_time'=>0,'modify_time'=>0],
        //     ['aid'=>0,'rid'=> 2,'first_time'=>0,'modify_time'=>0],
        //     ['aid'=>0,'rid'=> 3,'first_time'=>0,'modify_time'=>0],
        //     ['aid'=>0,'rid'=> 4,'first_time'=>0,'modify_time'=>0],
        // ];
        // AdminPathinfoAccountModel::insertAll($items);
        // return $info;
        // $data = [
        //     'service' => self::class.'@'.'test',
        //     'params' => [
        //         'id' => 1,
        //     ],

        // ];
        // MQService::pushDelay($data,15000,MQService::CHANNEL_OF_ACCOUNT);
        // MQService::pushDelay($data,5000,MQService::CHANNEL_OF_ACCOUNT);
        // MQService::pushDelay($data,3000,MQService::CHANNEL_OF_ACCOUNT);
        // return MQService::push($data,MQService::CHANNEL_OF_ACCOUNT);

    }

    public function test_callback_action(){
        $params = [
            "ext" => [
                "isoCode" => "CN",
                "phoneCode" => ""
            ],
            "package" => "com.enzhi.monitord",
            "code" => "721395",
            "receiver" => "15952607285",
            "vendor" => "ENZ",
            "sendType" => "phone",
            "language" => "zh-cn",
            "location" => "cn",
            "scene" => "register"
        ];
        SendCodeService::sendSMS($params);
    }

    public function test_sql_action(){
        $insert = [
            'iot_id' => '01GeY1xWdqG23yR8rAab000000',
            'user_id' => '514ba6fa88becaa30e9c25807beade3a',
            'vendor' => 'ENZ',
            'firsttime' => 1611986260,
            'modifytime' => 1611990474,
            'amqptime' => 0,
        ];
        $update = [  
            'vendor' => 'ENZ',
            'amqptime' => Helper::getMSTime(),
        ];
        $ret = UserDeviceBindingService::upsert($insert,$update);
    }

    public function test_do_auth_action(){
        // $user = [
        //     'token' => '01GeY1xWdqG23yR8rAab000000',
        //     'user_id' => '514ba6fa88becaa30e9c25807beade3a',
        // ];
        // $data = AliyunAccessService::doAuth($user);
        // return $data;
    }

    public function test_mask_action(){
        // return UserController::getMaskAccount("17688794855");

        return UserController::getMaskAccount("17@qq.com");
    }

    public function test_del_action(){
        // $params = [
        //     [
        //         'shareId' => '76078',
        //         'mainId' => '24Xo7ytumJqBAD0hx3UX000000',
        //     ],
        // ];
        
        // UserDeviceShareService::deleteAllShareByDevice($params); 
        UserDeviceShareService::delByFilter(['main_id' => null ]);
    }

    public function redis_lock_action(){
        $lockName = "test";
        $lockTime = 4;
        $time = 10;
        while($time > 0){
            if(RedisLockService::getLock($lockName,$lockTime)){
                CConsole::print_line("get ok ".$time);
            }
            else{
                CConsole::print_line("get failed ".$time);
            }
            sleep(1);
            $time--;
        }
    }

    public function ip_info_action($ip){
        $start = microtime(true);
        $res = IpLocation::info($ip);
        $end = microtime(true);
        CConsole::print_line(sprintf("used %f(second)",$end - $start));
        return $res;
    }

    public function test_reconnect_action(){        
        $devices = DeviceBaseService::getNativeAll([
            'server_site' => '',
            'aliyun_project_id' => ['in',[15]],
        ], null, 'id ASC', 0, 100);
        sleep(10);
        return DeviceBaseService::getNativeAll([
            'server_site' => '',
            'aliyun_project_id' => ['in',[15]],
        ], null, 'id ASC', 0, 10);    
    }

    public function test_send_record_action(){
        SendCodeHistoryService::addRecord("17688794855",SendCodeHistoryService::TYPE_OF_SMS,['code' => mt_rand(1000,99999)]);
        SendCodeHistoryService::addRecord("17688794055",SendCodeHistoryService::TYPE_OF_SMS,['code' => mt_rand(1000,99999)]);
        SendCodeHistoryService::addRecord("17612344855",SendCodeHistoryService::TYPE_OF_SMS,['code' => mt_rand(1000,99999)]);

    }

    public function test_exception_action(){
        throw new ApiException("just for test",Errno::COMMON_FREQUENT_REQUESTS_WITH_ERRORS);
    }

    public function test_push_text_mq_action(){
        NotifyDingTalkService::pushTextByAMQP("this is a test message!");
    }

    public function test_emoji_action(){
        $nickName = "242打34243😣😣😣😣😣😊😊fasdfasdf法师打发阿萨德";
        $len = strlen($nickName);
        $n = Helper::filterEmoji($nickName);
        var_dump($len,$n);
        if(Helper::hasEmoji($nickName)){
            return true;
        }
        return  false;
    }

    public function test_remove_action(){
        $params = [
            'admin_uid' => 166164,
            'iot_id' => 'f2Oc4xKy0zIyrOMegeJv000000',
        ];
        call_user_func(['app\\service\\UserDeviceShareService','doRealDestoryAsync'],$params);
        // UserDeviceShareService::doRealDestoryAsync();
    }

    public function send_email_to_guard_action(){

        throw new ApiException("only for test",Errno::COMMON_SERVICE_NOT_SUPPORT);

        // AmqpService::simplePush([            
        //     'service' => 'share\\service\\SendEmailProxy@send_to_guard',
        //     'params' => [
        //                 "test",
        //                 '[ error ] Exception detail: 
        //                     {
        //                         "error_code": 100002,
        //                         "error_msg": "encode data error",
        //                         "ip": "127.0.0.1",
        //                         "url": "/api/v1/toolkitUser/login",
        //                         "method": "POST",
        //                         "header": {
        //                             "content-length": "43",
        //                             "content-type": "application/json; charset=UTF-8",
        //                             "accept": "*/*",
        //                             "user-agent": "curl/8.4.0",
        //                             "host": "reaccount.enz.com"
        //                         },
        //                         "data": {
        //                             "account": "13312345678",
        //                             "pass": "12345678"
        //                         }
        //                     }'
        //         ],            
        //     ]
        // );
        // SendEmailProxy::send_to_guard("test","test");

    }
}